unit Main;

interface

uses
SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
	{ Private declarations }
public
	{ Public declarations }
end;

var
Form1: TForm1;

implementation
{$R *.DFM}

type
EBadStuff = class(Exception);  //Xay dng lp loi t tao

procedure Proc3;
begin
try
   	     //T  phat sinh loi
     raise EBadStuff.Create('Up the stack we go!');
finally
	ShowMessage('Exception raised. 
                                    Proc3 sees the exception');
end;
end;

procedure Proc2;
begin
try
	Proc3;
finally
	ShowMessage('Proc2 sees the exception');
end;
end;

procedure Proc1;
begin
try
	Proc2;
finally
	ShowMessage('Proc1 sees the exception');
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
	ExceptMsg = 'Exception message is"%s"';
begin
	ShowMessage('This method calls Proc1 
                               which calls Proc2 which calls Proc3');
	try
		Proc1;
	except
		on E:EBadStuff do
			ShowMessage(Format(ExceptMsg, [E.Message]));
	end;
end;

end.
